package com.tanjun.mallservice.web.service.impl;

import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tanjun.mallservice.apiservice.enums.plu.PluImgModeEnum;
import com.tanjun.mallservice.apiservice.enums.plu.PluImgTypeEnum;
import com.tanjun.mallservice.web.orm.entity.MallPluImg;
import com.tanjun.mallservice.web.orm.mapper.MallPluImgMapper;
import com.tanjun.mallservice.web.service.MallPluImgService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author jjtan
 * @description 针对表【mall_plu_img(商品图片)】的数据库操作Service实现
 * @createDate 2024-03-21 14:42:17
 */
@Service
public class MallPluImgServiceImpl extends ServiceImpl<MallPluImgMapper, MallPluImg>
        implements MallPluImgService {

    /**
     * 根据pluId和skuId获取图片
     *
     * @param pluId   商品id
     * @param skuId   规格id
     * @param imgType 图片类型
     * @param imgMode 图片模式
     * @return 返回图片
     */
    @Override
    @Cacheable(value = "getImg", key = "#pluId+'_'+#skuId+#imgType+#imgMode")
    public List<String> listImg(Long pluId, Long skuId, PluImgTypeEnum imgType, PluImgModeEnum imgMode) {
        return this.list(QueryChain.of(MallPluImg.class)
                .eq(MallPluImg::getPluId, pluId)
                .eq(MallPluImg::getSkuId, skuId,imgType == PluImgTypeEnum.SKU)
                .eq(MallPluImg::getImgType, imgType)
                .eq(MallPluImg::getImgMode, imgMode)
                .select(MallPluImg::getImgUrl)
        ).stream().map(MallPluImg::getImgUrl).collect(Collectors.toList());
    }

    /**
     * 根据pluId获取图片
     *
     * @param pluId 商品id
     * @return 返回图片
     */
    @Override
    @Cacheable(value = "listImgByPluId", key = "#pluId")
    public List<MallPluImg> listImgByPluId(Long pluId) {
        return this.list(QueryChain.of(MallPluImg.class)
                .eq(MallPluImg::getPluId, pluId)
        );
    }

    /**
     * 批量保存图片
     *
     * @param list
     */
    @Override
    @CacheEvict(cacheNames = {"listImgByPluId", "getImg"}, allEntries = true)
    public void saveImgs(List<MallPluImg> list) {
        List<MallPluImg> listOld = this.listImgByPluId(list.get(0).getPluId());
        //先判断哪些是删除的
        listOld.forEach(d -> {
            long count = list.stream().filter(t -> t.getImgMode() == d.getImgMode())
                    .filter(t -> t.getImgType() == d.getImgType())
                    .filter(t -> d.getImgType() == PluImgTypeEnum.SKU ? Objects.equals(t.getSkuId(), d.getSkuId()) : true)
                    .filter(t -> t.getImgUrl().equals(d.getImgUrl()))
                    .count();
            if (count == 0) { //不存在
                this.removeById(d.getPluImgId());
            }
        });

        //开始新增
        list.forEach(d -> {
            long count = listOld.stream().filter(t -> t.getImgMode() == d.getImgMode())
                    .filter(t -> t.getImgType() == d.getImgType())
                    .filter(t -> d.getImgType() == PluImgTypeEnum.SKU ? Objects.equals(t.getSkuId(), d.getSkuId()) : true)
                    .filter(t -> t.getImgUrl().equals(d.getImgUrl()))
                    .count();
            if (count == 0) { //不存在
                this.save(d);
            }
        });

    }

}




